//
//  HWHomeGameAlert.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/29.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWHomeGameAlert: MMPopupView {
    private var tapBgView:UIView!
    private var bgView: UIView!
    var gameButton:UIButton!
    
    var closeButton:UIButton!
    
    var closeButtonClick:(()->())?
    
    var gameButtonClick:((_:String)->())?
    
    var model:HWHomeAlertModel? {
        didSet {
            self.gameButton.sd_setImage(with: URL(string: model?.image ?? ""), for: .normal) { image, error, type, url in
                self.gameButton.snp.remakeConstraints { make in
                    make.left.right.equalToSuperview()
                    make.centerY.equalToSuperview()
                    make.height.equalTo(self.gameButton.snp.width).multipliedBy((image?.size.height ?? 0)/(image?.size.width ?? 0))
                }
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    override func hide() {
        super.hide()
        
    }
}
extension HWHomeGameAlert {
    private func initUI() -> Void {
        type = MMPopupType.alert
        backgroundColor = UIColor.clear
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
        gameButton = UIButton(type: .custom)
        gameButton.addTarget(self, action: #selector(gameButtonClick(_:)), for: .touchUpInside)
        addSubview(gameButton)
        
        closeButton = UIButton(type: .custom)
        closeButton.setImage(UIImage(named: "quxiaoico"), for: .normal)
        closeButton.addTarget(self, action: #selector(closeButtonClick(_:)), for: .touchUpInside)
        addSubview(closeButton)
        
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
//        bgView.snp.makeConstraints { make in
//            make.centerY.equalToSuperview()
//            make.left.equalTo(31*widthScale)
//            make.right.equalTo(-31*widthScale)
//
//        }
        
        gameButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-13*widthScale)
            make.top.equalTo(gameButton.snp.top).offset(-34)
            make.width.equalTo(26*widthScale)
            make.height.equalTo(69*widthScale)
        }
        
        
    }
}
extension HWHomeGameAlert {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        
//        hide()
        
    }
    
    @objc func gameButtonClick(_ button:UIButton) -> Void {
        hide()
        if model?.gid == "0"{
            
        } else {
            gameButtonClick?(model?.gid ?? "")
        }
    }
    
    @objc func closeButtonClick(_ button:UIButton) -> Void {
        hide()
//        closeButtonClick?()
    }
    
    
}
